package com.tamartech.greendb.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;

import com.tamartech.greendb.data.Person;


public class PersonDAO extends DAO
{
   public Person save (Person a) 
         throws Exception
   {
      try
      {
         begin();
         getSession().save(a);
         commit();
         return a;
      }
      catch (HibernateException e)
      {
         rollback();
         throw new Exception("Could not create person", e);
      }
   }


   public void update(Person a) throws Exception 
   {
      try 
      {
         begin();
         getSession().update(a);
//         getSession().save(a);
         commit();
      } 
      catch (HibernateException e) 
      {
         rollback();
         throw new Exception("Could not update person", e);
      }
   }

   public Person get (Long id) throws Exception 
   {
      try 
      {
         begin();
         Person a = (Person) getSession().load(Person.class, id);
         commit();
         return a;
      } 
      catch (HibernateException e) 
      {
         rollback();
         throw new Exception("Could not obtain person " + id, e);
      }
   }
/*
   public Person get (String name) throws Exception 
   {
      try 
      {
         begin();
         Query q = getSession().createQuery("from Person where name = :name");
         q.setString ("name", name);
         Person a = (Person) q.uniqueResult();
         commit();
         return a;
      } 
      catch (HibernateException e) 
      {
         rollback();
         throw new Exception("Could not obtain person " + name, e);
      }
   }
*/
   @SuppressWarnings("unchecked")
   public List<Person> list() throws Exception 
   {
      try 
      {
         begin();
         Query q = getSession().createQuery("from Person");
         List<Person> list = q.list();
         commit();
         return list;
      } 
      catch (HibernateException e) 
      {
         rollback();
         throw new Exception("Could not list the persons", e);
      }
   }

   public List<Person> findByName (Person p) throws Exception 
   {
      try 
      {
         begin();
         Query q = getSession().createQuery("from Person where surname = :surname " +
                                                          "and firstNames = :firstNames ");
         q.setString ("surname", p.getSurname());
         q.setString ("firstNames", p.getFirstNames());

         List<Person> found = q.list();
         commit();
         return found;
      } 
      catch (HibernateException e) 
      {
         rollback();
         throw new Exception("Could not obtain Persons " + p, e);
      }
   }
   
   public void delete(Person a)
      throws Exception
   {
      try
      {
         begin();
         getSession().delete(a);
         commit();
      }
      catch (HibernateException e)
      {
         rollback();
         throw new Exception("Could not delete person", e);
      }
   }
}
